﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DapperExtensions;
using DapperExtensions.Predicate;

namespace LS.Cores
{
    public abstract class BaseService<T> : IRepositoryBase<T> where T : class, new()
    {
        //基类配置 如多个数据库切换等
        /// <summary>
        /// 接口日志数据库
        /// </summary>
        public static ConnectionStringSettings CON_MYSQL_PLATLOG = ConfigurationManager.ConnectionStrings["MYSQL_LOG"];


        public ConnectionStringSettings GetDBConnectionString(string dbconstr)
        {
            if (!string.IsNullOrEmpty(dbconstr))
            {
                return ConfigurationManager.ConnectionStrings[dbconstr];
            }
            return null;
        }

        public static string GetStrSql(string strSplit)
        {
            if (string.IsNullOrEmpty(strSplit))
            {
                return string.Empty;
            }
            string[] aryval = strSplit.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            StringBuilder strsql = new StringBuilder();
            for (int i = 0; i < aryval.Length; i++)
            {
                strsql.Append("'" + aryval[i] + "',");
            }
            return System.Text.RegularExpressions.Regex.Replace(strsql.ToString(), ",$", "");
        }
        public int Update(T model, string dbconstr = null)
        {
            return DapperHelper.Update<T>(model, conString: GetDBConnectionString(dbconstr)) ? 1 : 0;
        }

        public T GetById(long id, string dbconstr = null)
        {
            return DapperHelper.GetById<T>(id, conString: GetDBConnectionString(dbconstr));
        }

        public int Delete(string ids, string dbconstr = null)
        {
            return DapperHelper.Delete<T>(ids, conString: GetDBConnectionString(dbconstr)) ? 1 : 0;
        }
        public int Recovery(string ids, string dbconstr = null)
        {
            return DapperHelper.Recovery<T>(ids, conString: GetDBConnectionString(dbconstr)) ? 1 : 0;
        }
        public int Remove(string ids, string dbconstr = null)
        {
            return DapperHelper.Remove<T>(ids, conString: GetDBConnectionString(dbconstr)) ? 1 : 0;
        }
        public IList<T> Query(string strwhere, string dbconstr = null)
        {
            return DapperHelper.Query<T>(strwhere, conString: GetDBConnectionString(dbconstr));
        }


        public PageDataView<T> GetPageList(string strwhere, int currentPage = 1, int pageSize = 20, string table = null, string fields = "*", string primaryKey = "id", string sort = "id", string dbconstr = null)
        {
            return DapperHelper.GetPageData<T>(currentPage, pageSize, strwhere, table, fields, primaryKey, sort, conString: GetDBConnectionString(dbconstr));
        }

        public long Insert(T model, string dbconstr = null)
        {
            return DapperHelper.Insert<T>(model, conString: GetDBConnectionString(dbconstr));
        }



        public DataTable QueryTable(string sql, string dbconstr = null)
        {
            return SqlHelper.QueryDataTable(sql, conString: GetDBConnectionString(dbconstr));
        }

        public object QueryScalar(string sql, object param = null, string dbconstr = null)
        {
            return SqlHelper.ExecuteScalar<object>(sql, param, conString: GetDBConnectionString(dbconstr));
        }
    }
}
